// JavaScript Document
$(document).ready(function(e) {
    queryByCondtion(
		1, 
		3, 
		[{property:"l.languageId", value:language, condition:"eq"},
		 {property:"n.newsStatus", value:1, condition:"eq"}
    ]);
	function queryByCondtion(pageNo, pageSize, params) {
		$.ajax ({
			url : ctx+"/news/search.do",
			type : "post",
			dataType:"json",	
			data : {
				pageNo : pageNo,
				pageSize : pageSize,
				orderType : "desc",
				orderBy : "isTop",
				rules : $.toJSON(params),
			},
			success : function(response) {
				updatePageLabel(response.pageNo,Math.ceil(response.totalCount/pageSize));
				query_result_handler(response.result);
			},
			error : function(response) {
				alert(response.status + " - " + response.statusText);
			}
		});
	}
	
	//查詢結果處理
	function query_result_handler(data) {
		$("#content").empty();
		if(data) {
			$.each(data,function(index, obj) {
				$("#content").append(
				$("<div>").addClass("unit")
				.append($("<div>").addClass("number").text("0" + (index+1)))
				.append($("<div>").addClass("title").attr("newsId",obj.newsId).text(obj.newsTitle).on("click",onNewsDetail))
				.append($("<div>").addClass("date").text(obj.newsDate))
				.append($("<div>").addClass("remark").attr("newsId",obj.newsId).text(obj.newsDesc).on("click",onNewsDetail))
				.append($("<div>").addClass("read").attr("newsId",obj.newsId).text("閱讀全文>>").on("click",onNewsDetail))
				.append($("<img>").attr("src",ctx+"/pages/web/res/product_parameter.png")));
			});
		}
	}
	
	//分頁處理 --- start
	var begin = 0;
	function updatePageLabel(pageNo, totalPage) {
		$("#tr_page").empty();
		if(totalPage == 0) {		
			return;
		}
		if(totalPage <= 5) {
			createPageLabel(1,(totalPage+1),pageNo);
		} else {
			if((pageNo < 5)) {
				createPageLabel(1,6,pageNo);
			} else { 
				if(((pageNo - 3) % 2) == 0) {
					begin = (pageNo + 2) > totalPage ? (totalPage - 4) : pageNo - 2;
				} 
				createPageLabel(begin,(begin+5),pageNo);
			}				
			if((pageNo + 2) < totalPage) {
				$("<td style='color:#666666'>...</td>").appendTo("#tr_page");
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:totalPage}).text(totalPage))
						 .appendTo("#tr_page");
			}
		}
		$("<td style='color:#666666'>&gt;</td>").appendTo("#tr_page");
		pageClick();
	}
	
	function createPageLabel(begin, end, pageNo) {
		$("<td style='color:#666666'>&lt;</td>").appendTo("#tr_page");
		for(var i=begin; i<end; i++) {
			if(i != pageNo) {
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:i}).text(i)).appendTo("#tr_page");
			} else {
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:i}).text(i).css("color","#F00"))
						 .appendTo("#tr_page");
			}
		}
	}
	
	function pageClick() {
		$("#tr_page a").on("click",function(e) {
			queryByCondtion(
				parseInt($(this).attr("title")), 
				3, 
				[{property:"l.languageId", value:language, condition:"eq"},
				 {property:"n.newsStatus", value:1, condition:"eq"}]);
			//updatePageLabel(parseInt($(this).attr("title")), 11);
		});
	}	
	//分頁處理end
	
	/**************************/
	function onNewsDetail(e) {
		window.open(ctx + "/news/web/newsDetail/" + $(this).attr("newsId") + ".do?languageId=" + language, "_self");
	}
	
});